<template>
  <div class="home-header">
    <div class="header-left">
      <div class="logo-container">
        <el-icon class="logo-icon"><Platform /></el-icon>
        <h2><slot name="title"></slot></h2>
      </div>
    </div>
    <div class="header-right">
      <el-tooltip
        effect="dark"
        content="系统设置"
        placement="bottom"
      >
        <div class="header-icon" @click="showSettingsMenu">
          <el-dropdown @command="handleCommand" trigger="click">
            <el-icon class="settings-icon"><Setting /></el-icon>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="userInfo">
                  <el-icon><User /></el-icon>
                  <span>个人信息</span>
                </el-dropdown-item>
                <el-dropdown-item divided command="logout">
                  <el-icon><SwitchButton /></el-icon>
                  <span>退出登录</span>
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-tooltip>
    </div>
  </div>
</template>

<script setup>
import { useUserStore } from '@/stores/user'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus' 

const userStore = useUserStore()
const router = useRouter()

const handleCommand = (command) => {
  switch (command) {
    case 'userInfo':
      router.push('/userinfo')
      break
    case 'logout':
      logout()
      break
  }
}

const logout = async () => {
  try {
    // 使用 ElMessageBox.confirm 而不是 ElMessage.confirm
    await ElMessageBox.confirm(
      '确定要退出登录吗？',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        draggable: true
      }
    )
    
    userStore.clearToken()
    userStore.userInfo = null
    router.push('/login')
    ElMessage.success('退出成功')
  } catch (error) {
    if (error !== 'cancel') {
      console.error('退出失败:', error)
      ElMessage.error('退出失败，请重试')
    }
  }
}
</script>

<style scoped>
.home-header {
  height: 60px;
  background: #f0f7ff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 24px;
  position: relative;
  z-index: 1000;
}

.header-left {
  display: flex;
  align-items: center;
}

.logo-container {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-icon {
  font-size: 28px;
  color: rgb(24,144,255);
}

.header-left h2 {
  margin: 0;
  color: rgb(24,144,255);
  font-size: 20px;
  font-weight: 500;
  letter-spacing: 0.5px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.header-icon {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.3s;
  background: rgba(255, 255, 255, 0.1);
}

.header-icon:hover {
  background: rgba(255, 255, 255, 0.2);
}

.settings-icon {
  font-size: 20px;
  color:rgb(24,144,255);
}

:deep(.el-dropdown-menu) {
  padding: 4px 0;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

:deep(.el-dropdown-menu__item) {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  font-size: 14px;
}

:deep(.el-dropdown-menu__item:not(.is-disabled):hover) {
  background-color: #f0f7ff;
  color: #1890ff;
}

:deep(.el-dropdown-menu__item .el-icon) {
  font-size: 16px;
}

/* 分割线样式 */
:deep(.el-dropdown-menu__item.is-divided) {
  border-top: 1px solid #f0f0f0;
  margin: 4px 0;
  padding-top: 12px;
}

/* 工具提示样式 */
:deep(.el-tooltip__trigger) {
  outline: none;
}
</style>